<?php
header("Access-Control-Allow-Origin:*"); //跨域名
header("Access-Control-Allow-Headers:*"); //跨端口
header("Content-type:Content-type: application/json;charset='utf-8'");
$raw = file_get_contents('php://input');//获取非表单数据
if($raw ){
    include '../config.php';
    $data =  json_decode($raw);
    $id = $data->id;
    $sql = '';
	//nav
    if ($id == 100) {
        $sql = "SELECT * FROM item";
    }
     //文章
    if ($id == 101) {
        $page = $data->page * 10;
        $sql = "SELECT id,title,book_img,timer,look_num,tag FROM book WHERE flg=1 ORDER BY id DESC LIMIT $page,10";
    }
     //tag
    if ($id == 102) {
        $sql = "SELECT * FROM tag WHERE flag=1";
    }
        //最近更新文章
    if ($id == 103) {
        $sql = "SELECT * FROM book WHERE flg=1 ORDER BY timer DESC limit 0, 10";
    }
    //查看最多
    if ($id == 104) {
        $sql = "SELECT * FROM book WHERE flg=1 order BY look_num+0 DESC limit 0, 10";
    }
    //友情链接
    if ($id == 105) {
        $sql = "SELECT * FROM link";
    }
    //搜索
    if ($id == 106) {
        $key = $data->key;
        $sql = "SELECT id,title,book_img,timer,look_num,tag FROM book WHERE flg=1 AND title like '%{$key}%' OR cont like '%{$key}%' OR tag like '%{$key}%'";
    }
    //上一条下一条
    if ($id == 200) {
        $ids = $data->id;
        $per = $ids - 1;
        $next = $ids + 1;
        $sql_1 = " SELECT * FROM book WHERE id='{$per}' AND flg=1";
        $sql_2 = " SELECT * FROM book WHERE id='{$next}' AND flg=1";
    }
	//获取通知
    if ($id == 107) {
        $sql = "SELECT * FROM msg WHERE flag=1 AND DATE_SUB(curdate(),INTERVAL 0 DAY) < timer AND  to_days(now()) >= to_days(new_timer)";
    }
	
	//文章文章详情
	if ($id == 201) {
	    $ids = $data->ids;
	    $sql = "SELECT id,title,book_img,timer,look_num,cont FROM book WHERE flg=1 AND id='{$ids}'";
	}
	//更新浏览数量
	if($id == 202){
	    $ids = $data->ids;
	    $sql = "UPDATE book SET look_num=look_num+1 WHERE id='{$ids}'";
	}
    //数据库配置文件
    if ($id == 200) {
        $arr = array();
        //新建一个数组
        $arr['pre'] = array();
        //存放数据
        $arr['next'] = array();
        //存放数据
        $stmt1 = $conn->query($sql_1);
        $stmt1->execute();
        $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
        foreach (new RecursiveArrayIterator($stmt1->fetchAll()) as $k1 => $v1) {
            array_push($arr['pre'], $v1);
            //添加到数组
        }
        $stmt2 = $conn->query($sql_2);
        $stmt2->execute();
        $result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
        foreach (new RecursiveArrayIterator($stmt2->fetchAll()) as $k2 => $v2) {
            array_push($arr['next'], $v2);
            //添加到数组
        }
        if ($result1 || $result2) {
            $arr['code'] = '200';
            $arr['msg'] = '获取数据成功';
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        } else {
            $arr['code'] = '400';
            $arr['msg'] = '获取数据失败';
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        }
    } else {
		
		if($id == 101){
			$stmt = $conn->prepare("SELECT id FROM book WHERE flg=1");
			$stmt->execute();
			$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
			$num = count($stmt->fetchAll());
		};
		if($id == 202){
			$stmt = $conn->prepare($sql);
			$stmt->execute();
			$stmt->setFetchMode(PDO::FETCH_ASSOC);
			$arr['data'] = true;
		}else{
			$stmt = $conn->prepare($sql);
			
			$stmt->execute();
			
			// 设置结果集为关联数组
			
			$arr = array();
			
			//新建一个数组
			
			$arr['data'] = array();
			
			//存放数据
			
			$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
			
			foreach (new RecursiveArrayIterator($stmt->fetchAll()) as $k => $v) {
			
			    array_push($arr['data'], $v);
			
			    //添加到数组
			
			}
			
			//便利数据库中的数据
		};
        if ($arr['data']) {
            $arr['code'] = '200';
            $arr['msg'] = '获取数据成功';
			if($id == 101){
				$arr['num'] = ceil($num/10);
			};
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        } else {
            $arr['code'] = '400';
            $arr['msg'] = '获取数据失败';
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        }
    }
}else{
    echo "<script>\r\n    window.location.href = '../';\r\n    </script>";
};
$conn = null;
?>